package com.zhku.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhku.base.exception.TaoTaoException;
import com.zhku.base.model.PageParams;
import com.zhku.base.model.PageResult;
import com.zhku.base.model.RestResponse;
import com.zhku.goods.feignclient.OrdersClient;
import com.zhku.goods.feignclient.TtUser;
import com.zhku.goods.feignclient.UserClient;
import com.zhku.goods.feignclient.VerifyEvaluateDto;
import com.zhku.goods.mapper.TtGoodsEvaluateMapper;
import com.zhku.goods.model.dto.EvaluateDto;
import com.zhku.goods.model.dto.ReturnEvaluateDto;
import com.zhku.goods.model.po.TtGoodsEvaluate;
import com.zhku.goods.service.TtGoodsEvaluateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TtGoodsEvaluateServiceImpl extends ServiceImpl<TtGoodsEvaluateMapper, TtGoodsEvaluate> implements TtGoodsEvaluateService {

    @Autowired
    TtGoodsEvaluateMapper ttGoodsEvaluateMapper;

    @Autowired
    OrdersClient ordersClient;

    @Autowired
    UserClient userClient;

    @Override
    @Transactional
    public TtGoodsEvaluate createEvaluate(EvaluateDto evaluateDto) {
        String userId = evaluateDto.getUserId();
        if (StringUtils.isEmpty(userId)) {
            throw new TaoTaoException("传入的用户id为空");
        }
        String goodsId = evaluateDto.getGoodsId();
        if (StringUtils.isEmpty(goodsId)) {
            throw new TaoTaoException("传入的商品id为空");
        }
        String evaluate = evaluateDto.getEvaluate();
        if (StringUtils.isEmpty(evaluate)) {
            throw new TaoTaoException("传入的评价内容为空");
        }
        //判断用户目前有没有评价资格
        VerifyEvaluateDto verifyEvaluateDto = new VerifyEvaluateDto();
        verifyEvaluateDto.setGoodsId(goodsId);
        verifyEvaluateDto.setUserId(userId);
        boolean b = ordersClient.verifyEvaluate(verifyEvaluateDto);
        if (!b) {
            throw new TaoTaoException("您目前没有购买该商品或还未确认收货，无法评价");
        }
        TtGoodsEvaluate ttGoodsEvaluate = new TtGoodsEvaluate();
        BeanUtils.copyProperties(evaluateDto,ttGoodsEvaluate);
        String id = UUID.randomUUID().toString();
        ttGoodsEvaluate.setId(id);
        int insert = ttGoodsEvaluateMapper.insert(ttGoodsEvaluate);
        if (insert <= 0) {
            throw new TaoTaoException("评价失败");
        }
        return ttGoodsEvaluate;
    }

    @Override
    @Transactional
    public void deleteEvaluate(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new TaoTaoException("传入的评价表id为空");
        }
        int i = ttGoodsEvaluateMapper.deleteById(id);
        if (i <= 0) {
            throw new TaoTaoException("删除评价失败");
        }
    }

    @Override
    public PageResult<ReturnEvaluateDto> getEvaluateList(PageParams pageParams, String id) {
        if (StringUtils.isEmpty(id)) {
            throw new TaoTaoException("传入的商品id为空");
        }
        LambdaQueryWrapper<TtGoodsEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TtGoodsEvaluate::getGoodsId,id);
        //创建page分页参数对象，参数：当前页码数，每页记录数
        Page<TtGoodsEvaluate> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        Page<TtGoodsEvaluate> ttShopPage = ttGoodsEvaluateMapper.selectPage(page, queryWrapper);
        List<TtGoodsEvaluate> items = ttShopPage.getRecords();
        List<ReturnEvaluateDto> items2 = new ArrayList<>();
        for (TtGoodsEvaluate item : items) {
            //获取用户id
            String userId = item.getUserId();
            //通过用户id查询每个进行评价的用户的名称
            RestResponse<TtUser> result = userClient.getUser(userId);
            TtUser user = result.getResult();
            if (user == null) {
                throw new TaoTaoException("查询某个商品的所有评价时，出现某个用户不存在异常");
            }
            ReturnEvaluateDto returnEvaluateDto = new ReturnEvaluateDto();
            BeanUtils.copyProperties(item,returnEvaluateDto);
            returnEvaluateDto.setName(user.getName());
            items2.add(returnEvaluateDto);
        }
        long counts = ttShopPage.getTotal();
        PageResult<ReturnEvaluateDto> ttShopPageResult = new PageResult<>(items2,
                counts, pageParams.getPageNo(), pageParams.getPageSize());
        return ttShopPageResult;
    }
}
